package com.hz.web.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hz.web.entity.RoleEntity;
import com.hz.web.entity.UserEntity;
import com.hz.web.service.IroleService;
import com.hz.web.service.IuserService;
import com.hz.web.util.ParmsEntity;

@Controller
@RequestMapping("user")
public class UserController {

	@Resource
	private IuserService userService;

	@Resource
	private IroleService roleService;
	
	/**
	 * 修改用户密码
	 * @return
	 * 
	 */
	@RequestMapping("updatePwd")
	@ResponseBody
	public Map<String, Object> updatePwd(HttpSession session,UserEntity user){
		Map<String, Object> resultMap = new HashMap<String, Object>();//结果map
		int row =-1;
		UserEntity entity = new UserEntity();
		if(session.getAttribute("userinfo")==null){
			resultMap.put("code", "404");
			resultMap.put("msg", "当前用户未登录,将跳转到登录页面！");
			resultMap.put("row", row);
			return resultMap;
		}else{
			//session数据
			entity = (UserEntity) session.getAttribute("userinfo");
			if(!entity.getUserName().equals(user.getUserName())){
				resultMap.put("code", "500");
				resultMap.put("msg", "修改密码失败！");
				resultMap.put("row", row);
				return resultMap;
			}
			if(!entity.getUserPass().equals(user.getUserPass())){
				resultMap.put("code", "500");
				resultMap.put("msg", "旧密码输入错误！");
				resultMap.put("row", row);
				return resultMap;
			}
			//再把输入的密码和session中的账户名一块去数据看看能不能查到数据
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("name", entity.getUserName());
			map.put("pass", user.getUserPass());
			try {
				UserEntity u = userService.queryUser(map);
				if(u==null){
					resultMap.put("code", "500");
					resultMap.put("msg", "修改失败，请联系管理员确认密码！");
					resultMap.put("row", row);
					return resultMap;
				}
			} catch (Exception e) {
				e.printStackTrace();
				resultMap.put("code", "500");
				resultMap.put("msg", "修改异常！");
				resultMap.put("row", row);
				return resultMap;
			}	
			try {
				row=userService.updatePwd(user);
			} catch (Exception e) {
				e.printStackTrace();
				resultMap.put("code", "500");
				resultMap.put("msg", "修改异常！");
				resultMap.put("row", row);
				return resultMap;
			}
			map.put("name", entity.getUserName());
			map.put("pass", user.getNewUserPass());
			UserEntity userEntity = userService.queryUser(map);
			session.getAttribute("userinfo");
			session.setAttribute("userinfo", userEntity);
			
			resultMap.put("code", "200");
			resultMap.put("msg", "修改密码成功！");
			resultMap.put("row", row);


			((UserEntity) session.getAttribute("userinfo")).getId();
			return resultMap;
		}
	}
	
	/**
	 * 插入用户信息
	 * @param entity
	 * @return
	 */
	@RequestMapping("insertUser")
	@ResponseBody
	public Map<String, Object>  insertUser(UserEntity entity){
		Map<String, Object> resultMap = new HashMap<String, Object>();//结果map
		int row =-1; 
		String username="";
		if(entity.getUserName()!=null ){
			username=entity.getUserName().replaceAll(" ", "");
			Map<String, Object> parmsMap = new HashMap<String, Object>();//参数map
			parmsMap.put("name", username);
			List<UserEntity> list =new ArrayList<UserEntity>();
			list=userService.queryUserList(parmsMap);
			if(list.size()>0){
				resultMap.put("row", row);
				resultMap.put("msg", "登录帐户已存在！");
				return resultMap;
			}
		}else{
			resultMap.put("row", row);
			resultMap.put("msg", "登录帐户为必填字段！");
			return resultMap;
		}
		
		entity.setUserName(username);
		entity.setUserPass(entity.getUserPass().replaceAll(" ", ""));
		entity.setShowName(entity.getShowName().replaceAll(" ", ""));
		entity.setIsDel("2");
		try {
			row=userService.insertUser(entity);
		} catch (Exception e) {
			e.printStackTrace();
			resultMap.put("row", row);
			resultMap.put("msg", "添加异常");
			return resultMap;
		}
		resultMap.put("row", row);
		resultMap.put("msg", "添加用户成功，审核通过后可登陆！");
		return resultMap;
	}
	
	/**
	 * 修改用户表 权限名称信息
	 * @param entity
	 * @return
	 */
	@RequestMapping("updateUser")
	@ResponseBody
	public Map<String, Object> updateUser(UserEntity entity){
		Map<String, Object> resultMap = new HashMap<String, Object>();//结果map
		int row =-1;
		int roleId = entity.getRoleId();
		Map<String, Object> parmsMap = new HashMap<String, Object>();
		parmsMap.put("roleId", roleId);
		List<RoleEntity> list = new ArrayList<RoleEntity>();
		try {
			//根据权限id查询权限名称  返回的是个只有一条数据的集合
			list=roleService.queryRole(parmsMap);
		} catch (Exception e) {
			e.printStackTrace();
			resultMap.put("msg", "根据权限id查询对应的权限名称异常！");
			resultMap.put("row", row);
			return resultMap;
		}
		if(list.size()>0){
			entity.setRoleName(list.get(0).getRoleName());
		}else{
			resultMap.put("msg", "根据权限id没有查询对应的权限名称！");
			resultMap.put("row", row);
			return resultMap;
		}
		try {
			row=userService.updateUser(entity);
		} catch (Exception e) {
			e.printStackTrace();
			resultMap.put("msg", "功能操作异常！");
			resultMap.put("row", row);
			return resultMap;
		}
		resultMap.put("row", row);
		return resultMap;
	}
	
	/**
	 * 查询
	 * @return
	 */
	@RequestMapping("queryUser")
	@ResponseBody
	public Map<String, Object> queryUser(ParmsEntity entity){
		Map<String, Object> resultMap = new HashMap<String, Object>();//结果map
		Map<String, Object> parmsMap = new HashMap<String, Object>();//参数map
		int page = entity.getPage();
		int rows = entity.getRows();
		if(page!=0 && rows!=0){
			int index = (page-1)*rows;
			int end =rows; 
			parmsMap.put("index", index);
			parmsMap.put("end", end);
		}
		
		List<UserEntity> list = new ArrayList<UserEntity>();
		String showName =entity.getShowName();
		String name = entity.getUserName();
		if(showName!=null && !"".equals(showName.trim())){
			parmsMap.put("adminShowName", "%"+showName.trim()+"%");
		}
		if(name!=null && !"".equals(name)){
			parmsMap.put("adminName", "%"+name.trim()+"%");
		}
		int count=0;
		list = userService.queryUserList(parmsMap);
		count = userService.queryUserCount(parmsMap);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		if(list.size()>0){
			for (UserEntity user : list) {
				if(user.getCreate_time()!=null){
					user.setCreateTimeStr(sdf.format(user.getCreate_time()));
				}	
			}
		}
		
		resultMap.put("rows", list);
		resultMap.put("total", count);
		return resultMap;
	}
	
}
